﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using InterestCalculationBiz;
using System.Web.Script.Serialization;
using System.IO;
using System.Text;
//using InterestCalculation;

namespace InterestCalculation.WebService
{
    /// <summary>
    /// Summary description for LoanCalculator
    /// </summary>
    public class LoanCalculator : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            
            context.Response.ContentType = "text/json";
            string method=context.Request.QueryString["method"].ToString();
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            try
            {
                switch (method)
                {
                    case "calculateLoan":
                        var jsonString = string.Empty;
                        JavaScriptSerializer jss = new JavaScriptSerializer();
                        context.Request.InputStream.Position = 0;
                        using (var inputStream = new StreamReader(context.Request.InputStream))
                        {
                            jsonString = inputStream.ReadToEnd();
                        }
                        var propObj = jss.Deserialize<LoanProperties>(jsonString);
                        context.Response.Write(GetJson(LoanHandler.CalculateLoan(propObj)));
                        break;
                }
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.ToString());
            }
        }
        /// <summary>
        /// Make json string from dataset for all table 
        /// </summary>
        /// <param name="ds">Pass whole dataset</param>
        /// <returns>json String</returns>
        public static string GetJson(DataSet ds)
        {
            StringBuilder sbjson = new StringBuilder();
            JavaScriptSerializer jss = new JavaScriptSerializer();
            Dictionary<string, object> dt = new Dictionary<string, object>();
            for (int i = 0; i < ds.Tables.Count; i++)
            {
                DataTable dts = ds.Tables[i];
                List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                foreach (DataRow dr in dts.Rows)
                {
                    Dictionary<string, object> data = new Dictionary<string, object>();
                    foreach (DataColumn col in dts.Columns)
                    {
                        data.Add(col.ColumnName, dr[col]);
                    }
                    rows.Add(data);
                }
                dt.Add(ds.Tables[i].TableName, rows);
            }
            sbjson.Append(jss.Serialize(dt));
            return sbjson.ToString();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}